package com.example.elasticsearch.controller;

import com.example.elasticsearch.entity.User;
import com.example.elasticsearch.service.EsServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(tags = "es")
@RestController
public class EsController {
    @Autowired
    EsServiceImpl esService;

    @PostMapping("/index")
    @ApiOperation("添加索引")
    public String addIndex(String indexName){
        try {
            esService.createIndex(indexName);
            return "添加索引成功";
        } catch (Exception e) {
            System.out.println("添加索引失败");
            return "添加索引失败";
        }
    }

    @DeleteMapping("/index")
    @ApiOperation("删除索引")
    public String deleteIndex(String indexName){
        try {
            boolean b = esService.deleteIndex(indexName);
            if(b){
                System.out.println("删除索引成功");
                return "删除索引成功";
            }else{
                System.out.println("删除索引失败");
                return "删除索引失败";
            }
        } catch (Exception e) {
            return "删除索引失败";
        }
    }

    @GetMapping("/index")
    @ApiOperation("判断索引是否存在")
    public String indexExist(String indexName){
        try {
            boolean b = esService.indexExist(indexName);
            if(b){
                System.out.println("索引存在");
                return "索引存在";
            }else{
                System.out.println("索引不存在");
                return "索引不存在";
            }
        } catch (Exception e) {
            System.out.println("error");
            return "索引不存在";
        }
    }

    @PostMapping("/index/doc")
    @ApiOperation("添加文档")
    public String addDoc(String indexName, User user){
        User user1 = new User();
        user1.setId(user.getId());
        user1.setName(user.getName());
        try {
            esService.insertOrUpdateOne(indexName,user1);
            return "添加文档成功";
        } catch (Exception e) {
            e.printStackTrace();
            return "添加文档失败";
        }
    }

    @GetMapping("/index/doc")
    @ApiOperation("查询文档")
    public ResponseEntity<List<User>> searchDoc(String indexName, String searchKey){
        try {
            TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery( "name.keyword",searchKey);
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            searchSourceBuilder.query(termQueryBuilder);
            List<User> userList = esService.search(indexName, searchSourceBuilder, User.class);
            return ResponseEntity.ok(userList);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok().build();
        }
    }

    @DeleteMapping("/index/doc")
    @ApiOperation("删除文档")
    public String deletDoc(String indexName, String deleteKey){
        try {
            QueryBuilder termQueryBuilder = QueryBuilders.termQuery("name",deleteKey);
            esService.deleteByQuery(indexName, termQueryBuilder);
            return "删除成功";
        } catch (Exception e) {
            e.printStackTrace();
            return "删除失败";
        }
    }
}
